package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zy.edu.bean.*;
import com.zy.edu.config.SystemParam;
import com.zy.edu.dao.SchoolDao;
import com.zy.edu.service.ClassinfoService;
import com.zy.edu.service.SchoolService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-07-13
 */
@Service
@Transactional
public class SchoolServiceImpl extends ServiceImpl<SchoolDao, School> implements SchoolService {
    @Resource
    UserService userService;

    @Resource
    ClassinfoService classinfoService;
    @Override
    public Map saveOrUpdate(School school) {
        List<School> tempList = this.baseMapper.selectList(new EntityWrapper<School>()
                .eq("name", school.getName()));
        if(StringUtils.isEmpty(school.getId())){
            school.setId("s-"+UUID.randomUUID().toString().replaceAll("-",""));
            school.setCreateTime(new Date());
            school.setStatus(1);
            if(tempList.size()>0){
                return ResponseInfo.error("学校名称已存在，不能创建");
            }else {
                this.baseMapper.insert(school);
            }
        }else{
            // 若存在名称相同的学校，且不是自己，则提示重复
            if(tempList.size()>0 && !tempList.get(0).getId().equals(school.getId())){
                return ResponseInfo.error("学校名称已存在，不能修改");
            }else {
                this.baseMapper.updateById(school);
            }
        }
        //更新静态数据
        SystemParam.SCHOOLS.put(school.getId(),school.getName());
        return ResponseInfo.success(null);
    }

    @Override
    public Map list(School school, InputParam inputParam) {
        EntityWrapper<School> entityWrapper = new EntityWrapper<School>();
        if(!StringUtils.isEmpty(school.getName())){
            entityWrapper.like("name",school.getName());
        }
        if(!StringUtils.isEmpty(school.getJurisId())){
            entityWrapper.eq("juris_id",school.getJurisId());
        }
        if(!StringUtils.isEmpty(school.getId())){
            entityWrapper.eq("id",school.getId());
        }
        entityWrapper.eq("status",1);
        entityWrapper.orderBy("create_time",false);

        //查分页
        if(inputParam!=null && inputParam.getPage()!=null && inputParam.getSize()!=null
                && inputParam.getPage()>0 &&  inputParam.getSize()>0){
            Page<School> page = new Page<School>(inputParam.getPage(), inputParam.getSize());
            List<School> tempList = this.baseMapper.selectPage(page,entityWrapper);
            Integer total = this.baseMapper.selectCount(entityWrapper);
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("rows", tempList);
            resultMap.put("total", total);
            return ResponseInfo.success(resultMap);
        }else {//查全部
            List<School> tempList = this.baseMapper.selectList(entityWrapper);
            return ResponseInfo.success(tempList);
        }
    }

    @Override
    public Map dele(School school) {
        if(!StringUtils.isEmpty(school.getId())){
            //设置为不可用
            school.setStatus(0);
            this.baseMapper.updateById(school);
            SystemParam.SCHOOLS.remove(school.getId());
            //同时删除下属的班级
            InputParam inputParam = new InputParam();
            inputParam.setSchoolId(school.getId());
            classinfoService.deleByJruisId(inputParam);
            userService.deleBySchoolId(inputParam);
        }
        return ResponseInfo.success(null);
    }

    @Override
    public List<School> findSchoolAndClassByJurisId(String jurisId){
        if(!StringUtils.isEmpty(jurisId)){
            return this.baseMapper.findSchoolAndClassByJurisId(jurisId);
        }else {
            return null;
        }
    }

    @Override
    public Map deleByJruisId(InputParam inputParam) {
        if(!StringUtils.isEmpty(inputParam.getJurisId())){
            inputParam.setStatus(0);
            this.baseMapper.deleByJruisId(inputParam);
            this.initSystemData();
            return ResponseInfo.success("");
        }else{
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
    }

    @Override
    public Map getByJurisId(String jurisId, String userId) {
        if(!StringUtils.isEmpty(jurisId)
                &&!StringUtils.isEmpty(userId)){
            User user = userService.selectById(userId);
            School school = new School();
            school.setJurisId(jurisId);
            //若是校管，教师或者学生，则只能查询自己所在的校区
            if(user.getRoleType().equals(Role.SCHOOL)
                ||user.getRoleType().equals(Role.TEACHER)
                    ||user.getRoleType().equals(Role.STUDENT)){
                school.setId(user.getSchoolId());
            }
            return this.list(school,null);

        }else{
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
    }

    @Override
    public void initSystemData() {
        SystemParam.SCHOOLS = new HashMap();
        List<School> schoolList = this.selectList(new EntityWrapper<School>()
                .eq("status",1));
        for (School school : schoolList) {
            SystemParam.SCHOOLS.put(school.getId(),school.getName());
        }
    }

    @Override
    public Map getById(School school, InputParam inputParam) {
        if(StringUtils.isEmpty(school.getId())) {
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
            return ResponseInfo.success(this.selectById(school.getId()));
    }

    @Override
    public Integer getCountByParam(InputParam inputParam) {

        EntityWrapper entityWrapper = new EntityWrapper<School>();
        entityWrapper.eq("juris_id", inputParam.getJurisId());
        entityWrapper.eq("status",1);
        Integer count = this.baseMapper.selectCount(entityWrapper);
        return count;
    }

}
